package com.sdx.console.promote.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.DateUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.promote.entity.PromoteConfig;
import com.sdx.console.promote.service.PromoteConfigService;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * 分销参数设置 接口
 */
@RequestMapping("/promote/promoteConfig")
@Controller
public class PromoteConfigController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(PromoteConfigController.class);
    private static final String MODEL = "/promote/promoteConfig";


    @Resource
	private PromoteConfigService promoteConfigService;


	/**
	 * 初始化页面需要的列表数据，
	 *
	 */
	private void setPageParam(ModelAndView view,HttpServletRequest request) {

	}

    /* 页面 */

    @GetMapping("/index.do")
    @Function("promote.promoteConfig.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
		setPageParam(view,request);
        return view;
    }

    @GetMapping("/edit.do")
    @Function("promote.promoteConfig.edit")
    @ResponseBody
    public ModelAndView edit(HttpServletRequest request,Integer tenantId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        PromoteConfig promoteConfig = promoteConfigService.queryById(tenantId);
        view.addObject("promoteConfig", promoteConfig);
		setPageParam(view,request);
		try {
			view.addObject("promoteConfigJson", JacksonUtils.toJsonString(promoteConfig,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("promote.promoteConfig.add")
    @ResponseBody
    public ModelAndView add(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
		setPageParam(view,request);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("promote.promoteConfig.query")
    @ResponseBody
    public JsonResult<PageQuery> list(PromoteConfig condtion,HttpServletRequest request)
    {
        CoreUser user = getLoginUser(request);
        condtion.setTenantId(user.getTenantId());
        PageQuery page = condtion.getPageQuery();
        promoteConfigService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,PromoteConfig promoteConfig,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		String dir = DateUtil.now("yyyyMMdd");
    	String subPath = "/images/cover/"+dir+"/";
		String bucketName = "sdxcomm";
		CoreUser user = getLoginUser(request);

    	if (isNew) {
			promoteConfig.setUpdatedBy(user.getId().intValue());
			promoteConfig.setUpdatedName(user.getName());
			promoteConfig.setUpdatedTime(new Date());
			promoteConfigService.save(promoteConfig);
		} else {
			promoteConfig.setUpdatedBy(user.getId().intValue());
			promoteConfig.setUpdatedName(user.getName());
			promoteConfig.setUpdatedTime(new Date());
			promoteConfigService.updateTemplate(promoteConfig);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("promote.promoteConfig.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)PromoteConfig promoteConfig,HttpServletRequest request
		)
    {

        return save(true,promoteConfig,request
		);
    }

    @PostMapping("/update.json")
    @Function("promote.promoteConfig.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  PromoteConfig promoteConfig,HttpServletRequest request
		) {
		return save(false,promoteConfig,request
		);
    }


   
    @GetMapping("/view.json")
    @Function("promote.promoteConfig.query")
    @ResponseBody
    public JsonResult<PromoteConfig>queryInfo(Integer tenantId) {
        PromoteConfig promoteConfig = promoteConfigService.queryById( tenantId);
        return  JsonResult.success(promoteConfig);
    }

    @PostMapping("/delete.json")
    @Function("promote.promoteConfig.del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        promoteConfigService.batchDelPromoteConfig(idList);
        return JsonResult.success();
    }
    

}
